<?php
namespace src;
use classlib\input;
use src\logic\ltags;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
class Csigner extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$dlang = C\lang::inst('dd');
		
		$view->assign('sex', $dlang->get('sh0003'));
		$view->assign('yes2no', $dlang->get('sh0007'));
		$view->assign('status', $dlang->get('sh0006'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/signer.ext.js');
	}

	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$orderby= '';
		list($start, $limit, $sorts) = Cadmin::offset();
		$sort   = $dir = '';
		if ($sorts) {
			list($sort, $dir) = each($sorts);
			if($sort && $dir && in_array($sort, array('userid', 'status', 'olstatus', 'reports'))) {
				$orderby = ' order by a.'.$sort.' '.$dir;
			}else if($sort && $dir && in_array($sort, array('xfans', 'charms', 'starlv', 'earningcoins', 'lastcoins', 'jointime'))) {
				$orderby = ' order by b.'.$sort.' '.$dir;
			}else if($sort && $dir && in_array($sort, array('salary'))) {
				$orderby = ' order by c.'.$sort.' '.$dir;
			}
		}
		$where = array();
		$query = input::post('query');
		$query = trim($query);
		if($query) {
			$query   = htmlspecialchars($query);
			$where[] = '(a.author like "%'.$query.'%" or a.title like "%'.$query.'%")';
		}
		$executor = input::post('executor');
		$executor = trim($executor);
		if($executor) {
			$executor= htmlspecialchars($executor);
			$where[] = 'c.executor like "%'.$executor.'%"';
		}
		$tagid = input::post('tagid');
		(is_numeric($tagid) && $tagid > 0) && ($where[] = 'a.tagids='.$tagid);
		$status = input::post('status');
		(is_numeric($status)) && ($where[] = 'a.status='.$status);
		$issign = input::post('issign');
		if (is_numeric($issign)) {
			$where[] = ($issign == 1)? 'a.origin = 1 AND c.userid > 0':'(a.origin=0 OR c.userid IS NULL)';
		}
		$sdate  = input::post('sdate');
		if ($sdate) {
			$where[] = 'b.jointime >= '.strtotime($sdate);
		}
		$edate  = input::post('edate');
		if ($edate) {
			$where[] = 'b.jointime < '.strtotime($edate);
		}
		$atable = C\model::load('pro.pl.anchor')->get_table();
		$stable = C\model::load('pro.pl.studio')->get_table();
		$btable = C\model::load('pro.sign.basic')->get_table();
		$eorm   = C\model::load('pro.pl.event');
		$wheres = empty($where)? '':' where '.implode(' AND ', $where);
		$field  = 'a.id, a.userid, a.title, a.thumb, a.olstatus, a.avertime, a.status, a.averviewer, a.studiono,'.
				'a.tagids, a.origin, a.reports, b.starlv, b.xfans, b.charms, b.lastcoins, b.earningcoins, b.salarycoins,'.
				' b.jointime, c.name as realname, c.userid as signid, c.age, c.sex, c.signtime, c.salary, c.executor';
		if ($sort && $dir && $sort == 'slivetime') {//按照时长排序的处理逻辑
			$wheres = ' left join (select userid, sum(lasttime - aritime - wastetime) as slivetime from '.
				$eorm->get_table().' where `status`=1 group by userid) as d on a.userid=d.userid'.$wheres;
			$field .= ', IFNULL(d.slivetime, 0)';
			$orderby = ' order by d.'.$sort.' '.$dir;
		}
		$sqltpl = 'select %s from %s as a left join %s as b on a.userid=b.userid'.
			' left join %s as c on a.userid=c.userid%s';
		$sql    = sprintf($sqltpl, 'count(1) as total', $stable, $atable, $btable, $wheres);
		$data   = C\model::load('pro.pl.studio')->get_assql($sql, true);
		$total  = ($data && isset($data['total']))? $data['total']:0;
		$list   = array();
		if ($total) {
			$sql  = sprintf($sqltpl, $field, $stable, $atable, $btable, $wheres).$orderby;
			$list = C\model::load('pro.pl.studio')->get_assql($sql, false, $start, $limit);
			$sorm = C\model::load('pro.inc.star');
			$ltime= 'sum(lasttime - aritime - wastetime) as livetime';
			 foreach ($list as &$item) {
			 	$item['lastcoins']= intval($item['lastcoins']);
			 	$item['salary']   = floatval($item['salary']);
			 	$item['user']     = Cadmin::user($item['userid']);
			 	$item['signtime'] = ($item['signtime'])? date('Y-m-d', $item['signtime']):'';
			 	$item['star']     = 'UnKnow';
			 	if ($item['starlv'] > 0) {
			 		$star         = $sorm->get_one($item['starlv']);
			 		$item['star'] = ($star)? $star['name'].'['.$star['star'].']':'UnKnow';
			 	}
			 	$item['events']   = C\router::get_url('admin/live/basic/event', 'main', array('userid'=>$item['userid'], 'status'=>1));
			 	$item['link']     = C\router::get_url('live', $item['studiono']);
			 	$tagids = empty($item['tagids'])? array():explode(',', $item['tagids']);
			 	$tagids = array_filter($tagids, 'is_numeric');
			 	$item['tags']     = 'UnKnow';
			 	if ($tagids) {
			 		$item['tags'] = ltags::stag_name($tagids);
			 	}
			 	$item['issign']   = ($item['signid'] && $item['origin'])? 1:0;
			 	if (isset($item['slivetime'])) {
			 		$item['livetime'] = Cadmin::__format_times($item['slivetime']);
			 	} else {
			 		$item['slivetime'] = $eorm->get_total(array('userid'=>$item['userid'], 'status'=>1), $ltime);
			 		$item['livetime']  = Cadmin::__format_times($item['slivetime']);
			 	}
				$item['jointime'] = date('Y-m-d H:i:s', $item['jointime']);
				$item['signinfo'] = C\router::get_url('admin/live/basic/sign', 'main', array('userid'=>$item['userid']));
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}